/**
 * Created by fly on 17/6/29.
 */
//闭包限定命名空间

var flyZommImg = function (_this, options) {
    this.options = options;
    this._this = _this;
    this._activity = false;
    this._opts = {
        imgSum: 0,
        rollSpeed: 200,//切换速度
        screenHeight: 165,//图片居中距离
        showBoxSpeed: 300,
        urlProperty: false,//指定data属性 如 urlProperty:src
        miscellaneous: true,//是否显示底部辅助按钮
        closeBtn: false,//是否打开右上角关闭按钮
        hideClass: false,//不需要显示预览的 class
        imgQuality:'original',//图片质量类型  thumb 缩略图 original 原图
        slitherCallback: function () {
        }//左滑动回调 两个参数 第一个动向 'left' 第二个 当前操作DOM
    },
        this._init = function () {

            var $this = this;

            $this._defaluts();

            $this._bind();

        },
        this._defaluts = function () {

            var opts = $.extend(this._opts, this.options || {});

            this.opts = opts;

            return opts;
        },
        this._bind = function () {

            var $this = this;

            $this._bindDom();

            $('body').on('click', '.fly-zoom-box', function (e) {

                $this._hideBox();

            });
            $('body').on('click', '.fly-zoom-box-close', function (e) {

                $this._hideBox();
                e.stopPropagation();
            });

            $('body').on('click', '.fly-zoom-box-restore', function (e) {

                $this._imgRestore('tap');
                e.stopPropagation();
            });

            $('body').on('click', '.fly-zoom-box-zoomout', function (e) {

                $this._zommImg(-100, 1);

                e.stopPropagation();
            });
            $('body').on('click', '.fly-zoom-box-zoom', function (e) {

                $this._zommImg(100, 1);

                e.stopPropagation();
            });

            $('body').on('click', '.fly-zoom-box-img', function (e) {
                $this._imgRestore('tap');
                e.stopPropagation();
            });

            $('body').on('click', '.fly-zoom-box-tool', function (e) {
                e.stopPropagation();
            });

            $('body').on('click', '.fly-zoom-box-label', function (e) {

                var $this_img = $(this);

                var group_name = $this_img.html();

                if (group_name && $this.groups && $this.groups[$this.group_name]) {

                    $this.group_name = group_name;

                    $this.opts.imgSum = $this.groups[$this.group_name].length;

                    $this.opts.img_index = 0;

                    $('.fly-zoom-box-fix').html(1);

                    $('.fly-zoom-box-length').html($this.opts.imgSum);

                    if($this.opts.urlProperty){
                        var src = $this.groups[$this.group_name][0].dom.data($this.opts.urlProperty);
                    }else {
                        var src =  $this.groups[$this.group_name][0].dom.attr('src');
                    }
                    $('.fly-zoom-box-img').attr('src',src);

                    $('.fly-zoom-box-label').css({
                        'background': '',
                        'color': '#666'
                    });

                    $this_img.css({
                        'background': 'rgba(62,69,80,1)',
                        'color': '#fff'
                    });

                    $this.oWidth = $this.oHeight = null;

                    $this._imgRestore('oneSwitcher', $this.groups[$this.group_name][0].dom);

                    $this.onPinch = $this.onRotate = null;
                }

                e.stopPropagation();
            });


        },
        this._bindDom = function () {

            var $this = this;

            $this.opts.imgSum = 0;
            $this.opts.img_index = 1;
            $this.groups = [];
            $this.group_names = [];
            $this.group_show = false;

            $this._this.find('img').each(function (index) {

                var img_this = $(this);

                if (($this.opts.hideClass && !img_this.hasClass($this.opts.hideClass)) || !$this.opts.hideClass) {


                    var group_name = img_this.data('group');
                    if (group_name) {
                        if (!$this.groups[group_name]) {
                            $this.group_names.push(group_name);
                            $this.groups[group_name] = [];
                        }
                        $this.groups[group_name].push({'dom': img_this});
                        $this.group_show = true;
                    }

                    if (!$this.group_show) {

                        img_this.data('index', $this.opts.imgSum);

                        $this.opts.imgSum += 1;

                    }
                }

            });

            if ($this.group_show) {
                var names = $this.group_names;
                for (var i = 0; i < names.length; i++) {
                    for (var k = 0; k < $this.groups[names[i]].length; k++) {
                        $this.groups[names[i]][k].dom.attr('data-index', k);
                    }
                }
            }


            this._this.find('img').on('click', function () {

                var domThis = $(this);

                if (($this.opts.hideClass && !domThis.hasClass($this.opts.hideClass)) || !$this.opts.hideClass) {


                    $this._flyBoxHtml(domThis);

                    $this._imgRestore('oneSwitcher', domThis);

                    $('body').on('touchmove', function (event) {//绑定默认事件，使滚动条不混动
                        event.preventDefault();
                    });

                    $this._touchBind($this);
                    if($this._activity) {
                        $this.opts.slitherCallback('firstClick', domThis);
                    }
                }

            });

        },
        this._reload = function () {

            $("body").unbind("touchmove");
            this._this.find('img').unbind('click');
            this._moveUnBind();
            this._bindDom();

        },
        this._flyBoxHtml = function (dom) {


            var index = parseInt(dom.data('index'));

            if (this.group_show) {

                this.group_name = dom.data('group');

                this.opts.imgSum = this.groups[this.group_name].length;

                this.opts.img_index = index;

            }

            var length = this.opts.imgSum;

            if(this.opts.urlProperty){
                var src =  dom.data(this.opts.urlProperty);
            }else {
                var src =  dom.attr('src');
            }
            var html = '';

            html += "<div class='fly-zoom-box' style='touch-action: none;display: none;-webkit-tap-highlight-color:rgba(255,255,255,0);cursor: pointer;position: fixed;z-index: 9999;width:100% ;height:100% ;background: rgba(20,20,20,1);top:0 ;bottom: 0;right:0 ;left:0 ;'>" +
                "<div class='fly-zoom-box-number' style='touch-action: none;z-index: 999999;position: absolute;top: 0;padding: 20px 0 ;line-height: 26px;color: #ddd;font-size: 14px;width: 100%;text-align: center;'><span style='background: rgba(255,255,255,0.2);border-radius: 15px;color: #fff;padding: 0px 6px'><span class='fly-zoom-box-fix'>" + (index + 1) + "</span>/<span class='fly-zoom-box-length'>" + length + "</span></span></div>" +
                "<div class='fly-zoom-box-main' style='touch-action: none;z-index: auto;position: relative;width: 100%;height: 100%;overflow: auto'><img class='fly-zoom-box-img' data-index='" + index + "' style='touch-action: none;display: block;width: 100%;position: absolute;' src='" + src + "'></div>";
            if (this.opts.closeBtn) {
                html += "<div class='fly-zoom-box-close' style='touch-action: none;text-align: center;z-index: 99999999;position: absolute;top: 11px;color: #ddd;font-size: 26px;right: 14px;'>×</div>";
            }

            if (this.opts.miscellaneous && this.group_names.length == 0) {

                html += "<div class='fly-zoom-box-tool' style='touch-action: none;z-index: 999999;position: absolute;bottom: 10px;padding: 10px 0 ;width: 200px;line-height: 26px;color: #ddd;font-size: 14px;margin: 0 auto;right: 0;left: 0;text-align: center;background: rgba(20,20,20,0.3);border-radius: 50px'><span class='fly-zoom-box-zoomout' style='background: rgba(255,255,255,0.2);border-radius: 15px;color: #fff;padding: 0px 6px'>－</span><span class='fly-zoom-box-restore' style='background: rgba(255,255,255,0.2);border-radius: 15px;color: #fff;padding: 2px 6px;margin: 0  0 0 10px '>还原</span><span class='fly-zoom-box-close' style='background: rgba(255,255,255,0.2);border-radius: 15px;color: #fff;padding: 2px 6px;margin: 0 10px'>关闭</span><span class='fly-zoom-box-zoom' style='background: rgba(255,255,255,0.2);border-radius: 15px;color: #fff;padding: 0px 6px'>＋</span></div>";

            }
            if (this.group_names.length > 0 && this.opts.miscellaneous) {
                html += "<div class='fly-zoom-box-tool' style='touch-action: none;text-align: center;z-index: 999999;position: absolute;bottom: 10px;padding: 10px 0 ;line-height: 26px;color: #ddd;font-size: 12px;margin: 0 auto;right: 0;left: 0;background: rgba(20,20,20,0.3);'>";

                var names = this.group_names;
                for (var i = 0; i < names.length; i++) {
                    html += "<span class='fly-zoom-box-label' style='width: max-content;border-radius: 5px;display: inline-block;";
                    if (names[i] == this.group_name) {
                        html += "background: rgba(62,69,80,1);color: #fff;";
                    } else {
                        html += "color: #666;";
                    }

                    html += "padding: 2px 9px;margin: 5px 10px 0 10px'>" + names[i] + "</span>";
                }

            }
            html += "</div>";

            $('body').append(html);

            this._showBox();

        },
        this._hideBox = function () {
            $('.fly-zoom-box').hide(this._opts.showBoxSpeed, 'linear', function () {
                $(this).remove();
            });
            $('body').unbind('touchmove'); //解绑事件，使滚动条不能混动

            if(this._activity){
                this.opts.slitherCallback('close', $('.fly-zoom-box-img'));
            }
            //左右滑动
            this._moveUnBind();

            this._activity = false;
        },
        this._showBox = function () {
            this._activity = true;
            $('.fly-zoom-box').show(this._opts.showBoxSpeed);
        },
        this._rightMove = function () {


            var $this = this;


            if ($this.group_show) {

                var index = $this.opts.img_index + 1;

                var length = $this.opts.imgSum;

                if (index >= length) {
                    index = 0;
                }
                $this.opts.img_index = index;

                var imgs = $this.groups[$this.group_name][index].dom;

            } else {
                var index = parseInt($('.fly-zoom-box-img').attr('data-index'));

                index = index + 1;

                var length = this.opts.imgSum;

                if (index >= length) {
                    index = 0;
                }
                var imgs = '';

                this._this.find('img').each(function () {

                    var img_this = $(this);

                    if (img_this.data('index') == index) {

                        imgs = img_this;

                        return false;
                    }

                });
            }


            $('.fly-zoom-box-fix').html(index + 1);

            $('.fly-zoom-box .fly-zoom-box-img').animate({left: "-200%"}, $this._opts.rollSpeed, 'linear', function () {

                $(this).remove();
            });

            if($this.opts.urlProperty){
                var src =  imgs.data($this.opts.urlProperty);
            }else {
                var src =  imgs.attr('src');
            }

            $('.fly-zoom-box-main').append("<img class='fly-zoom-box-img' data-index='" + index + "'  style='left:100%;display: block;position: absolute;' src='" + src + "'>");

            $this._imgRestore('chage', imgs);

            $('.fly-zoom-box-img').animate({left: "0%"}, $this._opts.rollSpeed, 'linear', function () {

                $this._touchBind($this);
                $this._imgRestore('switcher', imgs);

            });


            this._moveUnBind(index);
            if(this._activity) {
                this.opts.slitherCallback('left', imgs);
            }


        },
        this._leftMove = function () {

            var $this = this;

            if ($this.group_show) {

                var index = $this.opts.img_index - 1;

                var length = $this.opts.imgSum;

                if (index < 0) {
                    index = (length - 1);
                }

                $this.opts.img_index = index;

                var imgs = $this.groups[$this.group_name][index].dom;

            } else {
                var index = parseInt($('.fly-zoom-box-img').attr('data-index'));

                index = index - 1;

                var length = this.opts.imgSum;

                if (index < 0) {
                    index = (length - 1);
                }

                var imgs = '';

                this._this.find('img').each(function () {

                    var img_this = $(this);

                    if (img_this.data('index') == index) {

                        imgs = img_this;

                        return false;
                    }

                });
            }


            $('.fly-zoom-box-fix').html(index + 1);


            $('.fly-zoom-box-main .fly-zoom-box-img').animate({left: "200%"}, $this._opts.rollSpeed, 'linear', function () {

                $(this).remove();
            });

            if($this.opts.urlProperty){
                var src =  imgs.data($this.opts.urlProperty);
            }else {
                var src =  imgs.attr('src');
            }

            $('.fly-zoom-box-main').append("<img class='fly-zoom-box-img' data-index='" + index + "'  style='right:100%;display: block;position: absolute;' src='" + src + "'>");

            $this._imgRestore('chage', imgs);

            $('.fly-zoom-box-img').animate({right: "0%"}, $this._opts.rollSpeed, 'linear', function () {

                $this._touchBind($this);
                $this._imgRestore('switcher', imgs);


            });


            this._moveUnBind();
            if($this._activity) {
                this.opts.slitherCallback('right', imgs);
            }

        },
        this._moveUnBind = function () {

            //左右滑动
            $("body").unbind("touchstart");

            $("body").unbind("touchend");

        },
        this._touchBind = function ($this) {

            var X, Y;
            //左右滑动
            $("body").on("touchstart", function (e) {

                var point = e.originalEvent.touches ? e.originalEvent.touches[0] : e;
                $this.startX = point.pageX;
                $this.startY = point.pageY;
                window.clearTimeout($this.longTapTimeout);
                //两点接触
                if (e.originalEvent.touches.length > 1) {
                    var point2 = e.originalEvent.touches[1];
                    var xLen = Math.abs(point2.pageX - $this.startX);
                    var yLen = Math.abs(point2.pageY - $this.startY);
                    $this.touchDistance = $this._getDistance(xLen, yLen);
                    $this.touchVector = {
                        x: point2.pageX - $this.startX,
                        y: point2.pageY - $this.startY
                    };
                } else {
                    $this.startTime = $this._getTime();
                    $this.longTapTimeout = setTimeout(function () {
                        $this._emitEvent('longtap');
                    }, 800);
                    if ($this.previousTouchPoint) {
                        if (Math.abs($this.startX - $this.previousTouchPoint.startX) < 10 &&
                            Math.abs($this.startY - $this.previousTouchPoint.startY) < 10 &&
                            Math.abs($this.startTime - $this.previousTouchTime) < 500) {
                            $this._emitEvent('doubletap');
                        }
                    }
                    $this.previousTouchTime = $this.startTime;
                    $this.previousTouchPoint = {
                        startX: $this.startX,
                        startY: $this.startY
                    };
                }

            });

            $("body").on("touchmove", function (e) {

                var timestamp = $this._getTime();
                if (e.originalEvent.touches.length > 1) {

                    if ($this.touchVector) {
                        var vector = {
                            x: e.originalEvent.touches[1].pageX - e.originalEvent.touches[0].pageX,
                            y: e.originalEvent.touches[1].pageY - e.originalEvent.touches[0].pageY
                        };
                        var angle = $this._getRotateAngle(vector, $this.touchVector);
                        if (angle > 30) {
                            $this._emitEvent('rotate');
                            $this.touchVector.x = vector.x;
                            $this.touchVector.y = vector.y;
                        } else {
                            var xLen = Math.abs(e.originalEvent.touches[0].pageX - e.originalEvent.touches[1].pageX);
                            var yLen = Math.abs(e.originalEvent.touches[1].pageY - e.originalEvent.touches[1].pageY);
                            var touchDistance = $this._getDistance(xLen, yLen);

                            if ($this.touchDistance) {
                                var pinchScale = touchDistance / $this.touchDistance;
                                var pre = pinchScale - $this.previousPinchScale;
                                $this._emitEvent('pinch', {scale: pre});
                                $this.previousPinchScale = pinchScale;
                            }
                        }

                    }
                } else {
                    window.clearTimeout($this.longTapTimeout);
                    var point = e.originalEvent.touches ? e.originalEvent.touches[0] : e;
                    var deltaX = $this.moveX === null ? 0 : point.pageX - $this.moveX;
                    var deltaY = $this.moveY === null ? 0 : point.pageY - $this.moveY;
                    $this._emitEvent('move', {'deltaX': deltaX, 'deltaY': deltaY});

                    $this.moveX = point.pageX;
                    $this.moveY = point.pageY;
                }
                e.preventDefault();
            });

            $("body").on("touchend", function (e) {

                /**
                 * 在X轴或Y轴发生过移动
                 */
                window.clearTimeout($this.longTapTimeout);
                var timestamp = $this._getTime();

                X = $this.moveX - $this.startX;
                Y = $this.moveY - $this.startY;

                if ($this.moveX !== null && Math.abs(X) > 10 ||
                    $this.moveY !== null && Math.abs(Y) > 10) {

                    if (Math.abs(X) > Math.abs(Y) && X > 70) {
                        // alert("left 2 right");

                        $this._emitEvent('left');
                    }
                    else if (Math.abs(X) > Math.abs(Y) && X < -70) {
                        // alert("right 2 left");

                        $this._emitEvent('right');
                    }
                    else if (Math.abs(Y) > Math.abs(X) && Y > 70) {
                        // alert("top 2 bottom");
                        $this._emitEvent('bottom');
                    }
                    else if (Math.abs(Y) > Math.abs(X) && Y < -70) {
                        // alert("bottom 2 top");
                        $this._emitEvent('top');
                    }
                    else {
                        if (timestamp - $this.startTime < 500) {
                            $this._emitEvent('swipe');
                        }
                    }

                } else if (timestamp - $this.startTime < 2000) {
                    if (timestamp - $this.startTime < 500) {
                        $this._emitEvent('tap');
                    }
                    // if(timestamp - $this.startTime > 500) {
                    //     $this._emitEvent('长按');
                    // }
                }

                $this._emitEvent('touchend');

            });

        },
        this._zommImg = function (scale, number) {

            if (number <= 0) {
                if (isNaN(scale) || Math.abs(scale) > 0.2 || Math.abs(scale) < 0.02) {
                    return false;
                }
            }

            var imgDom = $('.fly-zoom-box-img');
            var oWidth = imgDom.width(); //取得图片的实际宽度
            var oHeight = imgDom.height(); //取得图片的实际高度


            var mWidth = window.screen.width; //取得图片的实际宽度
            var mHeight = window.screen.height - this._opts.screenHeight; //取得图片的实际高度

            if (number <= 0) {
                scale = scale * 2;
                var newWidth = oWidth + oWidth * scale;
                var newHeight = oHeight + oHeight * scale;


                var w = (mWidth - newWidth) / 2;
                var h = (mHeight - newHeight) / 2;
            } else {


                var newWidth = oWidth + scale;
                var newHeight = oHeight * (newWidth / oWidth);
                if (newWidth < mWidth) {
                    return false
                }
                if (scale > 0) {
                    this.onPinch = true;
                }
                var w = (mWidth - newWidth) / 2;
                var h = (mHeight - newHeight) / 2;

            }

            var  naturalWidth = '';
            var  naturalHeight = '';

            if(this._opts.imgQuality == 'original'){
                naturalWidth = imgDom[0].naturalWidth;
                naturalHeight = imgDom[0].naturalHeight;
            }else {
                naturalWidth = imgDom[0].width;
                naturalHeight = imgDom[0].height;

            }

            var offsetWidth = document.body.offsetWidth;
            var top = 50;

            var ch = (mHeight - naturalHeight) / 2;
            if (ch <= top) {
                ch = 70;
            }

            var dwidth = naturalWidth;
            var dheight = naturalHeight;

            var per = offsetWidth / dwidth;

            var dper = dwidth / dheight;


            if (dper < 1) {
                var o = mHeight / naturalHeight * 0.8;
                dwidth = dwidth * o;
                dheight = dheight * o;

                if (dwidth < (mWidth * 0.75)) {
                    dwidth = dwidth * (1 - dwidth / (mWidth * 0.75) + 1);
                    dheight = dheight * (1 - dheight / (mHeight * 0.75) + 1);

                }
                per = 1;
            }

            if(newWidth < dwidth*per){
                return false;
            }

            imgDom.width(newWidth);
            imgDom.height(newHeight);

            imgDom.css({'top': h + 'px', 'left': w + 'px'});


            return imgDom;
        },
        this._getTime = function () {
            return new Date().getTime();
        },
        this._getDistance = function (xLen, yLen) {
            return Math.sqrt(xLen * xLen + yLen * yLen);
        },
        this._getRotateDirection = function (vector1, vector2) {
            return vector1.x * vector2.y - vector2.x * vector1.y;
        },
        this._getRotateAngle = function (vector1, vector2) {
            var direction = this._getRotateDirection(vector1, vector2);
            direction = direction > 0 ? -1 : 1;
            var len1 = this._getDistance(vector1.x, vector1.y);
            var len2 = this._getDistance(vector2.x, vector2.y);
            var mr = len1 * len2;
            if (mr === 0) return 0;
            var dot = vector1.x * vector2.x + vector1.y * vector2.y;
            var r = dot / mr;
            if (r > 1) r = 1;
            if (r < -1) r = -1;
            return Math.acos(r) * direction * 180 / Math.PI;
        },
        this._setNumber = function (domthis, mWidth, mHeight, top, offsetWidth) {

            var naturalWidth;
            var naturalHeight;

            if (domthis) {
                if(this._opts.imgQuality == 'original'){
                    naturalWidth = domthis[0].naturalWidth;
                    naturalHeight = domthis[0].naturalHeight;
                }else {
                    naturalWidth = domthis[0].width;
                    naturalHeight = domthis[0].height;

                }

            }

            var ch = (mHeight - naturalHeight) / 2;
            if (ch <= top) {
                ch = 70;
            }

            var dwidth = naturalWidth;
            var dheight = naturalHeight;

            var per = offsetWidth / dwidth;

            var dper = dwidth / dheight;


            if (dper < 1) {
                var o = mHeight / naturalHeight * 0.8;
                dwidth = dwidth * o;
                dheight = dheight * o;

                if (dwidth < (mWidth * 0.75)) {
                    dwidth = dwidth * (1 - dwidth / (mWidth * 0.75) + 1);
                    dheight = dheight * (1 - dheight / (mHeight * 0.75) + 1);

                }
                per = 1;
            }
            return {'per': per, 'dwidth': dwidth, 'dheight': dheight, 'ch': ch};
        },
        this._imgRestore = function (type, domthis) {

            var $this = this;

            $this.cdomthis = domthis;

            var imgDom = $('.fly-zoom-box-img');

            var mWidth = window.screen.width; //取得图片的实际宽度
            var mHeight = window.screen.height - $this._opts.screenHeight; //取得图片的实际高度

            var w = (mWidth - $this.oWidth) / 2;
            var h = (mHeight - $this.oHeight) / 2;

            var offsetWidth = document.body.offsetWidth;
            var offsetHeight = document.body.offsetHeight;

            var top = 50;

            if (type == 'tap') {

                imgDom.css({
                    'top': $this.oTop + 'px',
                    'width': $this.oWidth + 'px',
                    'height': $this.oHeight + 'px',
                    'margin': '0 auto',
                    'right': '0%',
                    'left': '0%'
                });


                $this.onPinch = $this.onRotate = null;

            } else if (type == 'chage') {

                var $obj = $this._setNumber(domthis, mWidth, mHeight, top, offsetWidth);

                imgDom.css({
                    'top': $obj.ch + 'px',
                    'width': $obj.dwidth * $obj.per + 'px',
                    'height': $obj.dheight * $obj.per + 'px',
                    'margin': '0 auto'
                });

                $this.oTop = $obj.ch;
                $this.oWidth = $obj.dwidth * $obj.per;
                $this.oHeight = $obj.dheight * $obj.per;


            } else if (type == 'touchend') {

                if (imgDom.width() < $this.oWidth) {

                    var $obj = $this._setNumber(domthis, mWidth, mHeight, top, offsetWidth);

                    imgDom.animate({
                        'top': $obj.ch + 'px',
                        // 'width': $obj.dwidth * $obj.per + 'px',
                        // 'height': $obj.dheight * $obj.per + 'px',
                        'width': $obj.oWidth + 'px',
                        'height': $obj.oHeight + 'px',
                        'margin': '0 auto',
                        'right': '0%',
                        'left': '0%'

                    }, 80, 'linear', function () {
                        $this.onPinch = $this.onRotate = null;
                    });

                }

            } else if (type == 'switcher' || type == 'oneSwitcher') {


                if (type == 'oneSwitcher') {

                    var $obj = $this._setNumber(domthis, mWidth, mHeight, top, offsetWidth);

                    imgDom.css({
                        'top': $obj.ch + 'px',
                        'width': $obj.dwidth * $obj.per + 'px',
                        'height': $obj.dheight * $obj.per + 'px',
                        'margin': '0 auto',
                        'right': '0%',
                        'left': '0%'
                    });
                    $this.oTop = $obj.ch;
                    $this.oWidth = $obj.dwidth * $obj.per;
                    $this.oHeight = $obj.dheight * $obj.per;
                } else {
                    imgDom.css({
                        'right': '0%',
                        'left': '0%'
                    });
                }


            }


        },
        this._emitEvent = function (eventType, obj) {

            var $this = this;

            switch (eventType) {
                case 'tap':
                    // if(!$this.isPinch && !$this.isRotate){
                    //     $this.onTap = true;
                    //     // console.log('单击');
                    //     if((window.screen.height - $this.startY) > 200){
                    //         $this._imgRestore('tap');
                    //     }
                    //
                    //     return false;
                    // }
                    break;
                case 'doubletap':
                    // console.log('双击');
                    $this.onDoubletap = true;

                    break;
                case 'longtap':
                    $this.onLongtap = true;
                    // console.log('长按');
                    break;
                case 'swipe':
                    $this.onSwipe = true;
                    // console.log('挥');
                    break;
                case 'move':
                    if ($this.onPinch) {
                        $this.onMove = true;
                        // console.log('移动');
                        var imgDom = $('.fly-zoom-box-img');
                        var top = parseInt(imgDom.css('top')); //取得图片的实际宽度
                        var left = parseInt(imgDom.css('left')); //取得图片的实际高度

                        var newTop = top + obj.deltaY;
                        var newLeft = left + obj.deltaX;

                        imgDom.css({'top': newTop + 'px', 'left': newLeft + 'px'});

                    }
                    break;
                case 'pinch':
                    $this.onPinch = true;
                    $this.isPinch = true;
                    // console.log('缩放');
                    $this._zommImg(obj.scale, 0);
                    break;
                case 'rotate':
                    $this.isRotate = true;
                    $this.onRotate = true;
                    // console.log('旋转');
                    break;
                case 'left':
                    if (!$this.onPinch && !$this.onRotate) {
                        $this.onLeft = true;
                        $this._leftMove();
                        // console.log('左滑动');
                    }
                    break;
                case 'right':
                    if (!$this.onPinch && !$this.onRotate) {
                        $this.onRight = true;
                        $this._rightMove();
                        // console.log('右滑动');
                    }
                    break;
                case 'top':
                    if (!$this.onPinch && !$this.onRotate) {
                        $this.onTop = true;
                        // console.log('上滑动');
                        // $this._hideBox();
                    }
                    break;
                case 'bottom':
                    if (!$this.onPinch && !$this.onRotate) {
                        $this.onBottom = true;
                        // console.log('下滑动');
                        // $this._hideBox();
                    }
                    break;
                case 'touchend':
                    $this._imgRestore('touchend', $this.cdomthis);
                    // console.log($this.cdomthis);
                    $this.isPinch = $this.isRotate = $this.startX = $this.startY = $this.moveX = $this.moveY = $this.touchDistance = null;
                    $this.previousPinchScale = 1;

                    break;
            }
        }

};

$.fn.FlyZommImg = function (options) {
    var obj = new flyZommImg(this, options);
    obj._init();
    return obj;
};
